﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SimpleTaskManager.Domain.Abstract;
using SimpleTaskManager.Domain.Entities;

namespace SimpleTaskManager.WebUI.Controllers
{
    /// <summary>
    /// A controller which handles information about a user's tasks.
    /// </summary>
    public class MyTasksController : Controller
    {
        private IUserRepository uRepository;

        /// <summary>
        /// Creates an instance of the MyTasks Controller.
        /// </summary>
        /// <param name="userRepository">A repository containing the users.</param>
        public MyTasksController(IUserRepository userRepository)
        {
            uRepository = userRepository;
        }

        /// <summary>
        /// My tasks screen
        /// </summary>
        /// <returns>The rendered view.</returns>
        [Authorize]
        public ViewResult Index()
        {
            return View();
        }

        /// <summary>
        /// PartialView with table of user's tasks.
        /// </summary>
        /// <param name="id">The ID of the user, whose tasks will be displayed.</param>
        /// <returns>The rendered partial view.</returns>
        [Authorize]
        public ActionResult MyTasks(int id = 0)
        {
            User user = null;
            if (id == 0)
            {
                string username = User.Identity.Name;
                user = uRepository.Users.Single(x => x.Login.Equals(username));
            }
            else
            {
                user = uRepository.Users.SingleOrDefault(x => x.UserID == id);
            }

            if (user == null)
                return View("UserDoesntExist");

            return PartialView(user);
        }
    }
}
